$(function () {
  // todos 获取新闻列表
      $.ajax({
          method:'GET',
          url:'http://www.liulongbin.top:3006/api/news',
          success({status,msg,data}){
              console.log(data)
              
              if(status !== 200) return alert(msg)
              const newStr = data.map(item=>{
                  const {title,source,cmtcount,tags,img,time} = item 
                  //  时间格式转换
                  const date = new Date(time)
                  const times = date.toLocaleString()
                  // 利用正则将/转为-
                  const newTime = times.replace(/\//g,'-')
                  // 将字符串拆封为数组
                  const newData = tags.split(',')   
                  return `
                  <div class="news-item">
                  <img class="thumb" src="http://www.liulongbin.top:3006${img}" alt="" />
                  <div class="right-box">
                    <h1 class="title">${title}</h1>
                    <div class="tags">
                      <span>${newData[0] || ''}</span>
                      <span>${newData[1] || ''}</span>
                      <span>${newData[2] || ''}</span>
                    </div>
                    <div class="footer">
                      <div>
                        <span>${source}</span>&nbsp;&nbsp;
                        <span>${newTime}</span>
                      </div>
                      <span>评论数：${cmtcount}</span>
                    </div>
                  </div>
                </div>
                  `
              }).join('')
              $('#news-list').html(newStr)
          }
      })


})
